package FamilyTherapy;

import java.util.Scanner;

public class GCWindow 
{	
	public GCWindow()
	{
		//gcWindow constructed
	}
	
	public void displayWelcome()
	{
		GCWindow.outputStr("Welcome to the game! Let's start it!\n");
	}
	
	public int requestNoOfPlayer(Game g)
	{
		int input=100;

		input=GCWindow.inputInt(Main.noOfPlayerMin, Main.noOfPlayerMax, "Number of player ("+Main.noOfPlayerMin+"-"+Main.noOfPlayerMax+"): ");

		g.setNoOfPlayer(input);
		return g.getNoOfPlayer();
	}
	
	public void requestSetInfo(Game g, Player p[])
	{			
		for(int i=0; i<g.getNoOfPlayer() ;i++)
		{
			String iname = null;
			int irole = 100;
			
			iname=GCWindow.inputStr("Player " + i+ ", please input your name: \n");
			
			irole=GCWindow.inputInt(Main.roleMin, Main.roleMax, Player.getRoleNames()+ "\nPlayer " + i+ ", please select your role: \n");
			
			p[i] = new Player(iname, irole);		
		}
	}
	
	public void displayAllInfo(Player p[])
	{
		for(int i=0; i<p.length; i++)
		{
			displayPlayerInfo(p[i]);
		}	
		GCWindow.outputStr("=============================================\n");
	}

	public void displayPlayerInfo(Player p)
	{	
		GCWindow.outputStr("---------------------------------------------\n");
		GCWindow.outputStr(p.getName() + ", You are " + p.getRoleName()+"\n");
		GCWindow.outputStr("You have " + p.getPoint() + " self esteem points.\n\n");
	}
	
	public void requestUsingSA(SayActCard currentSA, int currentPlayerNo, Player p[])
	{
		char decision = 'y';
		int pt;

		decision=GCWindow.inputChar("Do you want to do this Say and Act task ? Enter \'y' es or \'n'o : \n");
		
		if( decision == 'y') 
		{
			decision=GCWindow.inputChar("Have you finished this task. Enter \'y' es or \'n'o : \n");
			
			if(decision == 'y' || decision == 'Y')
			{
				pt = currentSA.getPoint();
				p[currentPlayerNo].addPoint(pt);
				GCWindow.outputStr("You have been given " + pt + " point.\n");
			}
			else
			{
				GCWindow.outputStr("As you haven't finished this task, your SE point is deducted with 1 mark.\n");
				p[currentPlayerNo].deductPoint(1);
			}
		}
		else
		{
			GCWindow.outputStr("You do not do the task, so your SE point is deducted with 1 mark.\n");
			p[currentPlayerNo].deductPoint(1);;
		}	
	}
	
	public int requestUsingMT(int currentPlayerNo, Player p[]) //Edited by Sing 15/03/2012
	{
		char isUseMT = 'y';
		int useMTNum=100;
		boolean mtOK=false;
		
		isUseMT=GCWindow.inputChar("Does " + p[currentPlayerNo].getName()+ " want to use Magic Tool card? Enter \'y\'es or \'n\'o : \n");
		
		if(isUseMT=='y') { 
			if(p[currentPlayerNo].getHoldingCard().size() != 0) { 
				GCWindow.outputStr("You're currently holding: \n");
				GCWindow.outputStr(p[currentPlayerNo].getHoldingCard()+"\n");
				
				
				while (!mtOK) {					

					useMTNum = GCWindow.inputInt(Main.mtMin, Main.mtMax, "\nWhich one do you want to use? Please enter the card number: \n");
					for(int i=0;i<p[currentPlayerNo].getHoldingCard().size();i++)
					{	
						if(useMTNum==p[currentPlayerNo].getHoldingCard().get(i)){
							mtOK=true;
						}						
					}
					if(!mtOK)
						GCWindow.outputStr("[[Wrong input]]\n");
				}
					
				isUseMT = GCWindow.inputChar("\n[C]onfirm or [D]ismiss\n");
				return useMTNum;				
				
			}
			else {
				GCWindow.outputStr("Cannot use Magic Tool card as you have not held any card.\n");
			}
		}
		return -1;
	}
	
	//utilities -------------------------------------------------------------
	//for output
	public static void outputStr(String str)
	{
		System.out.print(str);
	}
	
	//for testing special cases automatically, 
	//such as assignDecisionMaker(which invoke the inputInt() more than once
	//private static Scanner scanner = new Scanner(System.in);
	
	//for input string
	public static String inputStr(String outStr)
	{
		//for testing normal cases 
		Scanner scanner = new Scanner(System.in);
		GCWindow.outputStr(outStr);
		return scanner.next();
	}
	
	//for input char
	public static char inputChar(String outStr)
	{
		//for testing normal cases 
		Scanner scanner = new Scanner(System.in);
		GCWindow.outputStr(outStr);
		return scanner.next().charAt(0);
	}	
	
	//for input int
	public static int inputInt(int min, int max, String outStr)
	{
		//for testing normal cases 
		Scanner scanner = new Scanner(System.in);
		int result=-1;
		
		do{
			if(scanner.hasNextInt())
			{
				if(!outStr.equals("NULL"))
					GCWindow.outputStr(outStr);
				
				result=scanner.nextInt();
				if(result<min ||result>max)
					GCWindow.outputStr("Please input a number between "+min+" and "+max+".\n");
			}
			else
			{
				GCWindow.outputStr("Please input an integer.\n");
				scanner.next();
			}
		}while(result<min || result >max);
		
		return result;
	}
}

